package com.cli.springboottemplateengine.engine;

import org.springframework.stereotype.Component;
import org.thymeleaf.context.Context;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.StringTemplateResolver;

import java.util.Map;

@Component
public class ThymeleafTemplateEngine implements TemplateEngine {

    private final org.thymeleaf.TemplateEngine engine;

    public ThymeleafTemplateEngine() {
        StringTemplateResolver resolver = new StringTemplateResolver();
        resolver.setTemplateMode(TemplateMode.TEXT); // 或 HTML
        resolver.setCacheable(false);
        resolver.setCheckExistence(true);

        this.engine = new org.thymeleaf.TemplateEngine();
        this.engine.setTemplateResolver(resolver);
    }

    @Override
    public String doRender(String templateContent, Map<String, Object> model) {
        Context context = new Context();
        context.setVariables(model);
        return engine.process(templateContent, context);
    }

    @Override
    public String getType() {
        return "thymeleaf";
    }
}
